package fr.umlv.galcon.planet;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;

import fr.umlv.galcon.item.ItemListSingleton;
import fr.umlv.galcon.player.PlayerType;
import fr.umlv.galcon.ship.ShipAbstract;

public class SelectorManager {
    private final Planet planet;

    private boolean isSelected;
    private boolean showSelection;

    private Ellipse2D selectionCircle;
    private int radius;

    /**
     * Constructor
     * 
     * @param planet
     *            The Planet to manage
     */
    public SelectorManager(Planet planet) {
	this.planet = planet;
	this.isSelected = false;
	this.showSelection = false;
	this.radius = 0;
    }

    /**
     * Display the CTRL + mouse wheel area selection.
     * 
     * @param g
     *            the graphics
     */
    public void areaSelection(Graphics2D g) {
	if (this.showSelection) {
	    this.selectionCircle = new Ellipse2D.Double(this.planet.getCenter().getX() - this.radius / 2, this.planet.getCenter()
		    .getY() - this.radius / 2, this.radius, this.radius);
	    g.setColor(Color.ORANGE);
	    g.draw(this.selectionCircle);

	    for (ShipAbstract ship : ItemListSingleton.getInstance().getShipList()) {
		if (this.selectionCircle.contains(ship.getCenter())
			&& ship.getSquadron().getOwner().getPlayerType() == PlayerType.HUMAN) {
		    ship.getShipSelectorManager().setWheelSelected(true);
		    ship.getSquadron().setSelected(true);
		}
	    }
	}
    }

    /**
     * Get the state selection of the Planet.
     * 
     * @return true if the planet is selected, else false
     */
    public boolean isSelected() {
	return this.isSelected;
    }

    /**
     * Raise the mouse wheel selection radius
     */
    public void raiseSelectionRadius() {
	if (this.radius < this.planet.getSize() * 4)
	    this.radius += 20;
    }

    /**
     * Reduce the mouse wheel selection radius
     */
    public void reduceSelectionRadius() {
	if (this.radius >= this.planet.getSize()) {
	    this.radius -= 20;
	} else {
	    this.showSelection = false;
	}
    }

    /**
     * Set the selection of the planet
     * 
     * @param selected
     *            state
     */
    public void setSelected(boolean selected) {
	this.isSelected = selected;
    }

    /**
     * Set if the CTRL + mouse wheel selection has to be shown.
     * 
     * @param showSelection
     *            A boolean to shown the selection or not.
     */
    public void setShowSelection(boolean showSelection) {
	this.showSelection = showSelection;
	if (!this.showSelection)
	    this.radius = 0;
    }
}
